package com.ai.dylan.spring.mcp.controller;

import com.ai.dylan.spring.mcp.entity.BookInfo;
import com.ai.dylan.spring.mcp.service.BookService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/books")
@RequiredArgsConstructor
public class BookController {

  @Resource
  private BookService bookService;
  @GetMapping("init")
  public ResponseEntity<String> initBooks() {
    List<BookInfo> sampleBooks = Arrays.asList(
            new BookInfo(null, "Spring实战（第6版）", "编程", "Craig Walls",
                    LocalDate.of(2022, 1, 15), "9787115582247"),
            new BookInfo(null, "深入理解Java虚拟机", "编程", "周志明",
                    LocalDate.of(2019, 12, 1), "9787111641247"),
            new BookInfo(null, "Java编程思想（第4版）", "编程", "Bruce Eckel",
                    LocalDate.of(2007, 6, 1), "9787111213826"),
            new BookInfo(null, "算法（第4版）", "计算机科学", "Robert Sedgewick",
                    LocalDate.of(2012, 10, 1), "9787115293800"),
            new BookInfo(null, "云原生架构", "架构设计", "张三",
                    LocalDate.of(2023, 3, 15), "9781234567890"),
            new BookInfo(null, "微服务设计模式", "架构设计", "张三",
                    LocalDate.of(2021, 8, 20), "9789876543210"),
            new BookInfo(null, "领域驱动设计", "架构设计", "Eric Evans",
                    LocalDate.of(2010, 4, 10), "9787111214748"),
            new BookInfo(null, "高性能MySQL", "数据库", "Baron Schwartz",
                    LocalDate.of(2013, 5, 25), "9787111464747"),
            new BookInfo(null, "Redis实战", "数据库", "Josiah L. Carlson",
                    LocalDate.of(2015, 9, 30), "9787115419378"),
            new BookInfo(null, "深入浅出Docker", "容器技术", "李四",
                    LocalDate.of(2022, 11, 20), "9787123456789"));
    bookService.saveBatch(sampleBooks);
    return ResponseEntity.ok("Books initialized successfully");
  }
  @GetMapping("/search/title")
  public ResponseEntity<List<BookInfo>> findByTitle(@RequestParam String title) {
    List<BookInfo> books = bookService.findByTitle(title);
    return ResponseEntity.ok(books);
  }

  @GetMapping("/search/author")
  public ResponseEntity<List<BookInfo>> findByAuthor(@RequestParam String author) {
    List<BookInfo> books = bookService.findByAuthor(author);
    return ResponseEntity.ok(books);
  }

  @GetMapping("/search/category")
  public ResponseEntity<List<BookInfo>> findByCategory(@RequestParam String category) {
    List<BookInfo> books = bookService.findByCategory(category);
    return ResponseEntity.ok(books);
  }
}
